require([ "dojo/parser", "dojo/dom", "dojo/_base/fx", "dojo/ready",
		"dijit/registry", "dojo/request", "dojo/on", "dojo/dom-form",
		"dojo/json", "dojo/store/Memory", "gridx/Grid",
		"gridx/core/model/cache/Async", "dojox/data/QueryReadStore",
		"dijit/TitlePane", "gridx/allModules", "dijit/Toolbar",
		"dijit/form/Button", "dijit/Dialog", "dijit/layout/TabContainer",
		"dijit/layout/ContentPane", "dojo/domReady!" ], function(parser, dom,
		fx, ready, registry, request, on, domForm, JSON, Store, Grid, Cache,
		QueryReadStore, TitlePane) {
	store = new QueryReadStore({
		url : "query.action",
		requestMethod : "post"
	});
	console.log(store);
	// store = new Store({
	// data : [ {
	// id : 1,
	// name : "帐号1",
	// account : "account_1",
	// remark : "备注1",
	// enable : "1"
	// }, {
	// id : 2,
	// name : "帐号2",
	// account : "account_2",
	// remark : "备注2",
	// enable : "2"
	// }, {
	// id : 3,
	// name :"帐号3",
	// account:"account_3",
	// remark:"备注3",
	// enable:"3"
	// } ]
	// });

	layout = [ {
		field : "id",
		name : "流水号"
	}, {
		field : "name",
		name : "姓名"
	}, {
		field : "account",
		name : "帐号"
	}, {
		field : "remark",
		name : "备注"
	}, {
		field : "enable",
		name : "有效否"
	} ];

	gridTop = [ {
		plugin : 'userToolbar'
	} ];

	ready(function() {
		setTimeout(function() {
			parser.parse();

			var btnAddUser = registry.byId("addUser");
			var callback = function() {
				// registry.byId("userDialog").show();
				// artDialog.open("safe/resource/show.action", {
				// id : "uu"
				// }, false);
				var userDialog = registry.byId("userDialog");
				userDialog.show();
			};
			var handle = on(btnAddUser, "click", callback);

			registry.byId("modifyUser").on("click", function(evt) {
				var userGrid = registry.byId("userGrid");
				var userGridStore = userGrid.store;
				var rowIds = userGrid.select.row.getSelected();
				var item = userGrid.row(rowIds[0]).item();
				var _userId = userGridStore.getValue(item, "id");
				request.post("get.action", {
					handleAs : "json",
					data : {
						id : _userId
					}
				}).then(function(_userData) {
					for ( var key in _userData) {
						if (registry.byId(key)) {
							registry.byId(key).set("value", _userData[key]);
						}
					}
					var userDialog = registry.byId("userDialog");
					// var userDialog = registry.byId("userDialog").placeAt(
					// top.document.body);
					userDialog.show();
				}, function(error) {
					alert(error);
				});
			});

			registry.byId("btnSave").on("click", function(evt) {
				var formData = domForm.toJson("userBaseForm");
				var formDataObject = JSON.parse(formData);
				var url = "add.action";
				var _id = formDataObject.id;
				if (_id && _id != "" && _id.length == 32) {
					url = "modify.action";
				}
				request.post(url, {
					handleAs : "json",
					data : formDataObject
				}).then(function(response) {
					var ds = new QueryReadStore({
						url : "query.action",
						requestMethod : "post"
					});
					registry.byId("userGrid").setStore(ds);
				}, function(error) {
					alert(error);
				});
			});

			registry.byId("deleteUser").on("click", function(evt) {
				var userGrid = registry.byId("userGrid");
				var userGridStore = userGrid.store;
				var rowIds = userGrid.select.row.getSelected();
				var idArray = new Array();
				for ( var i = 0; i < rowIds.length; i++) {
					var item = userGrid.row(rowIds[i]).item();
					idArray.push(userGridStore.getValue(item, "id"));
				}

				request.post("delete.action", {
					handleAs : "json",
					data : {
						idList : idArray
					}
				}).then(function(response) {
					var ds = new QueryReadStore({
						url : "query.action",
						requestMethod : "post"
					});
					userGrid.setStore(ds);
				}, function(error) {
					alert(error);
				});
			});

			registry.byId("authorize").on(
					"click",
					function(evt) {
						var roleSetting = {
							data : {
								simpleData : {
									enable : true,
									idKey : "id",
									pIdKey : "pid"
								}
							},
							check : {
								chkboxType : {
									"Y" : "p",
									"N" : "ps"
								},
								enable : true
							}
						};

						request.post("../role/tree.action", {
							handleAs : "json"
						}).then(
								function(_roleTreeData) {
									$.fn.zTree.init($("#roleTree"),
											roleSetting, _roleTreeData);
									var zTree = $.fn.zTree
											.getZTreeObj("roleTree");
									var rootNode = zTree.getNodeByParam("id",
											"root");
									rootNode.nocheck = true;
									zTree.updateNode(rootNode);
									registry.byId("authorizeDialog").show();
									
									//one user
									
									var userGrid = registry.byId("userGrid");
									var rowIds = userGrid.select.row.getSelected();
									
									if (rowIds && rowIds.length == 1) {
										var item = userGrid.row(rowIds[0]).item();
										var userGridStore = userGrid.store;
										var _userId = userGridStore.getValue(item, "id")
										zTree.checkAllNodes(false);
										request.post("getRelatingRole.action", {
											handleAs : "json",
											data:{userId:_userId}
										}).then(function(_roleCheckData) {
											if (_roleCheckData) {
												for(var i =0; i<_roleCheckData.length; i++) {
													var _node = zTree.getNodeByParam("id",  _roleCheckData[i]);
													zTree.checkNode(_node, true, false);
												}
											}
										}, function(error) {
											alert(error);
										});											
									}
									
								}, function(error) {
									alert(error);
								});
					});

			registry.byId("btnSaveAuthorize").on("click", function(evt) {
				var userGrid = registry.byId("userGrid");
				var userGridStore = userGrid.store;
				var rowIds = userGrid.select.row.getSelected();
				var idArray = new Array();
				for ( var i = 0; i < rowIds.length; i++) {
					var item = userGrid.row(rowIds[i]).item();
					idArray.push(userGridStore.getValue(item, "id"));
				}

				if (idArray.length > 0) {
					var roleTreeObj = $.fn.zTree.getZTreeObj("roleTree");
					var roles = roleTreeObj.getCheckedNodes(true);
					if (roles && roles.length > 0) {
						var roleArray = new Array();
						for ( var i = 0; i < roles.length; i++) {
							roleArray.push(roles[i].id);
						}

						request.post("relatingRole.action", {
							handleAs : "json",
							data : {
								userIdList : idArray,
								roleIdList : roleArray
							}
						}).then(function(response) {
						}, function(error) {
							alert(error);
						});
					} else {
						// 取消授权
						request.post("cancelRelatingRole.action", {
							handleAs : "json",
							data : {
								userIdList : idArray
							}
						}).then(function(response) {
						}, function(error) {
							alert(error);
						});
					}
				} else {
					alert("请选择要授权的用户");
				}
			});

			setTimeout(function hideLoader() {
				fx.fadeOut({
					node : 'loader',
					duration : 500,
					onEnd : function(n) {
						n.style.display = "none";
					}
				}).play();
			}, 100);

		}, 200);

	});
});